package com.springboot.hiresystem.controller;

import com.springboot.hiresystem.dto.resp.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

@RestController
@RequestMapping("/file")
public class FileController {
    @PostMapping("/upload")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.failure(404,"File is empty.");
        }
        //获取文件后缀名
        try {
            System.out.println("---------"+file.getOriginalFilename());
            // 保存文件
            Path filePath = Path.of("F:/a learning specidic floder/homework/hire-system/src/main/resources/static/upload/"+file.getOriginalFilename());
            Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);

            return Result.success(file.getOriginalFilename());
        } catch (IOException e) {
            System.out.println("文件上传失败"+e);
            return Result.failure(500,"上传失败: " + e.getMessage());
        }
    }
}